﻿using System.Collections.Generic;

using Ah.Testudin.Engine.Actors;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Ah.Testudin.Engine.Graphics.Scenes.SceneNodes
{
    public class PrimitiveSceneNode : SceneNodeBase
    {
        #region Private Member Variables

        private readonly Effect _effect;

        private readonly VertexPositionColorNormal[] _vertices;

        private readonly int[] _indices;

        #endregion

        #region Constructors

        public PrimitiveSceneNode(
            ActorId actorId,
            RenderPass renderPass,
            Matrix toWorldTransform,
            Effect effect,
            VertexPositionColorNormal[] vertices,
            int[] indices)
            : base("Primitive Scene Node", actorId, renderPass, toWorldTransform)
        {
            _effect = effect;

            _vertices = vertices;

            _indices = indices;
        }

        #endregion

        #region SceneNodeBase

        public override void Render(Scene scene)
        {
            scene.Renderer.RasterizerState = RasterizerState.CullNone;

            scene.Renderer.BlendState = BlendState.Opaque;

            scene.Renderer.DepthStencilState = new DepthStencilState()
            {
                DepthBufferEnable = true
            };

            _effect.CurrentTechnique = _effect.Techniques["ColoredNoShading"];

            _effect.Parameters["xView"].SetValue(scene.Camera.View);

            _effect.Parameters["xProjection"].SetValue(scene.Camera.Projection);

            _effect.Parameters["xWorld"].SetValue(this.Properties.ToWorldTransform);

            foreach (EffectPass pass in _effect.CurrentTechnique.Passes)
            {
                pass.Apply();
            }

            scene.Renderer.DrawUserIndexedPrimitives<VertexPositionColorNormal>(
                PrimitiveType.TriangleList,
                _vertices,
                0,
                _vertices.Length,
                _indices,
                0,
                _indices.Length / 3);
        }

        #endregion
    }
}
